home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / FLTK-1.0.6 / src / Fl_Adjuster.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-07  |  3.9 KB  |  135 lines

  1. //
  2. // "$Id: Fl_Adjuster.cxx,v 1.5 1999/01/07 19:17:16 mike Exp $"
  3. //
  4. // Adjuster widget for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-1999 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems to "fltk-bugs@easysw.com".
  24. //
  25.  
  26.  
  27. #include <FL/Fl.H>
  28. #include <FL/Fl_Adjuster.H>
  29. #include <FL/Fl_Bitmap.H>
  30. #include <FL/fl_draw.H>
  31.  
  32. #include "fastarrow.h"
  33. static Fl_Bitmap fastarrow(fastarrow_bits, fastarrow_width, fastarrow_height);
  34. #include "mediumarrow.h"
  35. static Fl_Bitmap mediumarrow(mediumarrow_bits, mediumarrow_width, mediumarrow_height);
  36. #include "slowarrow.h"
  37. static Fl_Bitmap slowarrow(slowarrow_bits, slowarrow_width, slowarrow_height);
  38.  
  39. // changing the value does not change the appearance:
  40. void Fl_Adjuster::value_damage() {}
  41.  
  42. void Fl_Adjuster::draw() {
  43.   int dx, dy, W, H;
  44.   if (w()>=h()) {
  45.     dx = W = w()/3;
  46.     dy = 0; H = h();
  47.   } else {
  48.     dx = 0; W = w();
  49.     dy = H = h()/3;
  50.   }
  51.   draw_box(drag==1?FL_DOWN_BOX:box(), x(),  y()+2*dy, W, H, color());
  52.   draw_box(drag==2?FL_DOWN_BOX:box(), x()+dx, y()+dy, W, H, color());
  53.   draw_box(drag==3?FL_DOWN_BOX:box(), x()+2*dx,  y(), W, H, color());
  54.   if (active_r())
  55.     fl_color(selection_color());
  56.   else
  57.     fl_color(inactive(selection_color()));
  58.   fastarrow.draw(x()+(W-fastarrow_width)/2,
  59.          y()+2*dy+(H-fastarrow_height)/2, W, H);
  60.   mediumarrow.draw(x()+dx+(W-mediumarrow_width)/2,
  61.            y()+dy+(H-mediumarrow_height)/2, W, H);
  62.   slowarrow.draw(x()+2*dx+(W-slowarrow_width)/2,
  63.          y()+(H-slowarrow_width)/2, W, H);
  64. }
  65.  
  66. int Fl_Adjuster::handle(int event) {
  67.   double v;
  68.   int delta;
  69.   int mx = Fl::event_x();
  70.   switch (event) {
  71.   case FL_PUSH:
  72.     ix = mx;
  73.     if (w()>=h())
  74.       drag = 3*(mx-x())/w() + 1;
  75.     else
  76.       drag = 3-3*(Fl::event_y()-y()-1)/h();
  77.     handle_push();
  78.     redraw();
  79.     return 1;
  80.   case FL_DRAG:
  81.     if (w() >= h()) {
  82.       delta = x()+(drag-1)*w()/3;    // left edge of button
  83.       if (mx < delta)
  84.     delta = mx-delta;
  85.       else if (mx > (delta+w()/3)) // right edge of button
  86.     delta = mx-delta-w()/3;
  87.       else
  88.     delta = 0;
  89.     } else {
  90.       if (mx < x())
  91.     delta = mx-x();
  92.       else if (mx > (x()+w()))
  93.     delta = mx-x()-w();
  94.       else
  95.     delta = 0;
  96.     }
  97.     switch (drag) {
  98.     case 3: v = increment(previous_value(), delta); break;
  99.     case 2: v = increment(previous_value(), delta*10); break;
  100.     default:v = increment(previous_value(), delta*100); break;
  101.     }
  102.     handle_drag(soft() ? softclamp(v) : clamp(v));
  103.     return 1;
  104.   case FL_RELEASE:
  105.     if (Fl::event_is_click()) { // detect click but no drag
  106.       if (Fl::event_state()&0xF0000) delta = -10;
  107.       else delta = 10;
  108.       switch (drag) {
  109.       case 3: v = increment(previous_value(), delta); break;
  110.       case 2: v = increment(previous_value(), delta*10); break;
  111.       default:v = increment(previous_value(), delta*100); break;
  112.       }
  113.       handle_drag(soft() ? softclamp(v) : clamp(v));
  114.     }
  115.     drag = 0;
  116.     redraw();
  117.     handle_release();
  118.     return 1;
  119.   }
  120.   return 0;
  121. }
  122.  
  123. Fl_Adjuster::Fl_Adjuster(int x, int y, int w, int h, const char* l)
  124.   : Fl_Valuator(x, y, w, h, l) {
  125.   box(FL_UP_BOX);
  126.   step(1, 10000);
  127.   selection_color(FL_BLACK);
  128.   drag = 0;
  129.   soft_ = 1;
  130. }
  131.  
  132. //
  133. // End of "$Id: Fl_Adjuster.cxx,v 1.5 1999/01/07 19:17:16 mike Exp $".
  134. //
  135.